

use serde_json::Value;

use crate::prelude::ColumnCellLeaf;


pub fn render_cell(
    leafs: &[&ColumnCellLeaf],
    row_data: Vec<Value>,
) -> Vec<Value> {
    if leafs.is_empty() || row_data.is_empty() {
        return vec![];
    }
    let is_perc=leafs.iter().enumerate().any(|(i, leaf)| {
        if leaf.name == "dw" {
            if let Value::String(v)=&row_data[i] {
                return v == "%";
            }
        }
        false
    });
    let mut result = Vec::with_capacity(leafs.len());
    for v in row_data.into_iter() {
        if is_perc {
            match v {
                Value::Number(number) => {
                    if let Some(num) = number.as_f64() {
                        result.push(Value::String(format!("{:.2}%", num * 100.0)));
                    } else {
                        result.push(Value::from(number));
                    }
                },
                _=>result.push(v),
            }
        }else{
            result.push(v);
        }
    }
    result
}